// 公司编辑修改角色及权限
import * as roleApi from '@/api/role'

const state = {
  // 获取修改角色信息
  personageroleList: {},
  // 权限列表
  permissionList: []
}
const mutations = {
  SET_PERSONAGEROLE_LIST(state, newValue) {
    state.personageroleList = newValue
  },
  SET_PERMISSION_LIST(state, newValue) {
    state.permissionList = newValue
  }
}

const actions = {
  // 获取修改角色信息
  async updatepersonageroleList({ commit }, id) {
    const res = await roleApi.getRole(id)
    commit('SET_PERSONAGEROLE_LIST', res)
  },
  // 权限列表
  async updatePermissionList({ commit }, id) {
    const res = await roleApi.getPermission(id)
    commit('SET_PERMISSION_LIST', res)
  }
}

function tranfrom(data, pid) {
  return data.filter(d => d.pid === pid)
  .map(i => ({
    ...i,
    children: tranfrom(data, i.id)
  }))
}

// 处理权限列表
const getters = {
  permission(state) {
    return tranfrom(state.permissionList, '0')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
